/**
 * @description 后台首页
 *
 * @author xiaomi
 */

const appName = 'admin';

const readFrontRoutes = require('../../../../app/utils/read-front-routes');
const pathToRegexp = require('path-to-regexp');
const logger = require('../../../../app/services/logger');
const PATHS = require('../../../../webpack.paths')({
    appName: appName
});
const getManifest = require('../../../services/get-manifest');

module.exports = function(app) {
    const env = process.env;
    const router = app.router;
    const IS_PROD = env.NODE_ENV === 'production';
    // const Manifest = app.models.Manifest;
    const cdnBaseUrl = PATHS.PUBLIC;

    const homeMW = function *() {
        const user = this.req.user || null;
        const pathNames = this.path.split('/').slice(1).map(name => {
            if(!name) {
                return 'index';
            }

            if(+name > 0) {
                return 'item';
            }

            return name;
        });

        const manifestsMap = yield getManifest('admin');
        yield this.render('index', {
            env: env,
            path: this.path,
            pathNames: pathNames,
            user: user,

            cdnBaseUrl: cdnBaseUrl,
            manifestsMap: manifestsMap,
            getHashUrl(uri) {
                return IS_PROD && manifestsMap[uri] || uri;
            },
            getCdnUrl(uri) {
                uri = this.getHashUrl(uri);

                return cdnBaseUrl + uri;
            }
        });
    };

    // Front routes
    readFrontRoutes(`views/${appName}/routes.js`)
    .map(item => {
        const redirect = item.redirect || '';
        const path = item.path || '';

        if(!path || path === '*') {
            return;
        }

        if(redirect) {
            const toPath = pathToRegexp.compile(redirect);
            const statusCode = +item.statusCode || 302;

            router.get(path, function *() {
                const urlData = this._parsedUrl || {};
                const url = toPath(this.params) + (urlData.search || '');

                this.status = statusCode;
                this.redirect(url);
            });

            return;
        }
        router.get(path, homeMW);
    })
    .catch(err => {
        logger.error(err);

        process.exit(1);
    });
    // router.get('/', homeMW);
    // router.get('/login', homeMW);
    // router.get('/templets', homeMW);
    // router.get('/templets/:id', homeMW);
    // router.get('/attributes', homeMW);
    // router.get('/attributes/:id', homeMW);
    // router.get('/materials', homeMW);
    // router.get('/materials/:id', homeMW);
    // router.get('/materials/design/:type', homeMW);
    // router.get('/designers', homeMW);
    // router.get('/designers/:id', homeMW);
    // router.get('/fonts', homeMW);
    // router.get('/fonts/:id', homeMW);
    // router.get('/tags', homeMW);
    // router.get('/tags/:id', homeMW);
    // router.get('/tags/:id/:type', homeMW);
    // router.get('/feedbacks', homeMW);
    // router.get('/feedbacks/export', homeMW);
    // router.get('/notifications', homeMW);
    // router.get('/notifications/:id', homeMW);
    // router.get('/banners', homeMW);
    // router.get('/banners/:id', homeMW);
    // router.get('/posts', homeMW);
    // router.get('/posts/*', homeMW);
    // router.get('/editors', homeMW);
    // router.get('/editors/*', homeMW);
    // router.get('/offlines/*', homeMW);
    // router.get('/offlines', homeMW);
    // router.get('/products', homeMW);
    // router.get('/products/*', homeMW);
    // router.get('/users', homeMW);
    // router.get('/bills', homeMW);
    // router.get('/orders', homeMW);
    // router.get('/config', homeMW);
    // router.get('/keywords', homeMW);
    // router.get('/keyword-recommends', homeMW);
    // router.get('/keywords/:id', homeMW);
    // router.get('/keyword-groups', homeMW);
    // router.get('/keyword-materials', homeMW);
    // router.get('/keyword-materials/:id', homeMW);
    // router.get('/keyword-groups/:id', homeMW);

    // 前台
    router.get('/www', function * () {
        let url = this.protocol + '://';
        url += env.WWW_HOST;

        this.redirect(url);
    });
};
